package com.tong.lib.utils;

import androidx.annotation.NonNull;
import android.util.Log;

import com.tong.lib.BuildConfig;

/**
 * Created by hjt on 2019/8/13.
 */

public class LogUtils {

    private static String TAG = "";
    private static String className;//类名
    private static String methodName;//方法名
    private static int lineNumber;//行数

    /**
     * 判断是否可以调试
     *
     * @return
     */
    public static boolean isDebuggable() {
        return BuildConfig.DEBUG;
    }

    /**
     * 获取文件名、方法名、所在行数
     *
     * @param sElements
     */
    private static void getMethodNames(StackTraceElement[] sElements) {
        className = sElements[1].getFileName();
        methodName = sElements[1].getMethodName();
        lineNumber = sElements[1].getLineNumber();
    }

    @NonNull
    private static String creatLog(String log) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("=====");
        buffer.append(methodName);
        buffer.append("(").append(className).append(":").append(lineNumber).append(")=====:");
        if (!TAG.equals("")) {
            buffer.append(TAG + "--->");
            TAG = "";
        }
        buffer.append(log);
        return buffer.toString();
    }

    public static LogUtils t(String Tag) {
        TAG = Tag;
        return null;
    }

    public static void v(String msg) {
        if (!isDebuggable()) return;
        getMethodNames(new Throwable().getStackTrace());
        Log.v(className, creatLog(msg));
    }

    public static void d(String msg) {
        if (!isDebuggable()) return;
        getMethodNames(new Throwable().getStackTrace());
        Log.d(className, creatLog(msg));
    }

    public static void i(String msg) {
        if (!isDebuggable()) return;
        getMethodNames(new Throwable().getStackTrace());
        Log.i(className, creatLog(msg));
    }

    public static void w(String msg) {
        if (!isDebuggable()) return;
        getMethodNames(new Throwable().getStackTrace());
        Log.w(className, creatLog(msg));
    }

    public static void e(String msg) {
        if (!isDebuggable()) return;
        getMethodNames(new Throwable().getStackTrace());
        Log.e(className, creatLog(msg));
    }
}
